import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
* @description: 1282. 用户分组
* @author hewei
* @date 2022/8/12 14:30
* @version 1.0
*/

public class GroupThePeople {

    public List<List<Integer>> groupThePeople(int[] groupSizes) {
        HashMap<Integer, List<Integer>> map = new HashMap<>();
        List<List<Integer>> ans = new ArrayList<>();
        for (int i = 0; i < groupSizes.length; i++) {
            int key = groupSizes[i];
            if (!map.containsKey(key)) {
                List<Integer> list = new ArrayList<>();
                list.add(i);
                map.put(key, list);
            } else {
                map.get(key).add(i);
            }
            if (map.get(key).size() == key) {
                ans.add(new ArrayList<>(map.get(key)));
                map.remove(key);
            }
        }
        return ans;
    }
}
